import { login, sysProfile, sysUser } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setToken(value)
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  removeToken (state) {
    state.token = ''
    setToken('')
    // token失效 清除用户信息
    state.userInfo = ''
  }
}
const actions = {
  async toLogin (store, value) {
    const res = await login(value)
    store.commit('setToken', res.data)
    console.log(res)
  },
  async getUserInfo ({ commit }) {
    const res = await sysProfile()
    // 获取头像信息的调用
    const res2 = await sysUser(res.data.userId)
    commit('setUserInfo', { ...res.data, ...res2.data })
    console.log('用户信息', res, res2)
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
